package nl.gamayun.whohasit.utils;

import java.util.Calendar;

import nl.gamayun.whohasit.R;
import nl.gamayun.whohasit.WhoHasIt;
import android.content.res.Resources;

public class DurationFormat {
	
	public static enum FORMAT{
		FULL, SIMPLE
	}
	
	public static enum DIRECTION{
		FROMTODAY, TILLTODAY
	}
	
	static final private String SPACE = " ";
	static final public long ONE_SECOND = 1000;
	static final public long SECONDS = 60;
	static final public long ONE_MINUTE = ONE_SECOND * 60;
	static final public long MINUTES = 60;
	static final public long ONE_HOUR = ONE_MINUTE * 60;
	static final public long HOURS = 24;
	static final public long ONE_DAY = ONE_HOUR * 24;
	static final public long ONE_MONTH = ONE_DAY * 30;
	static final public long ONE_YEAR = ONE_MONTH * 12;
	
	private static Resources resources = WhoHasIt.getContext().getResources();
	
	public static String getDurationFromTodayBig(Calendar date) {
		return getDurationFrom(date, FORMAT.FULL, DIRECTION.FROMTODAY);
	}

	public static String getDurationFromTodaySmall(Calendar date) {
		return getDurationFrom(date, FORMAT.SIMPLE, DIRECTION.FROMTODAY);
	}
	
	public static String getDurationTillTodayBig(Calendar date) {
		return getDurationFrom(date, FORMAT.FULL, DIRECTION.TILLTODAY);
	}

	public static String getDurationTillTodaySmall(Calendar date) {
		return getDurationFrom(date, FORMAT.SIMPLE, DIRECTION.TILLTODAY);
	}

	public static String getDurationFrom(Calendar date, FORMAT format, DIRECTION direction) {
		Calendar today = Calendar.getInstance();
		today.set(Calendar.HOUR, 0);
		today.set(Calendar.MINUTE, 0);
		today.set(Calendar.SECOND, 0);
		today.set(Calendar.MILLISECOND, 0);
		date.set(Calendar.HOUR, 0);
		date.set(Calendar.MINUTE, 0);
		date.set(Calendar.SECOND, 0);
		date.set(Calendar.MILLISECOND, 0);
		StringBuffer res = new StringBuffer();
		long duration;
		if (direction.equals(DIRECTION.FROMTODAY))
		{
			duration = date.getTimeInMillis() - today.getTimeInMillis();
			if (duration > 0)
			{
				res.append(resources.getString(R.string.duration_due));
			}
			else
			{
				res.append(resources.getString(R.string.duration_to_late));
				duration = duration * -1;
			}
			
		}
		else
		{
			duration = today.getTimeInMillis() - date.getTimeInMillis();
			res.append(resources.getString(R.string.duration_for));
		}
		long temp = 0;
		if (duration >= ONE_SECOND) {
			temp = duration / ONE_YEAR;
			if (temp > 0) {
				duration -= temp * ONE_YEAR;
				res.append(SPACE+temp).append((format.equals(FORMAT.SIMPLE)?resources.getString(R.string.duration_y):temp > 1 ? SPACE+resources.getString(R.string.duration_years) : SPACE+resources.getString(R.string.duration_year)))
					.append(duration >= ONE_MINUTE ? ", " : "");
			}

			temp = duration / ONE_MONTH;
			if (temp > 0) {
				duration -= temp * ONE_MONTH;
				res.append(SPACE+temp).append((format.equals(FORMAT.SIMPLE)?resources.getString(R.string.duration_m):temp > 1 ? SPACE+resources.getString(R.string.duration_months) : SPACE+resources.getString(R.string.duration_month)))
					.append(duration >= ONE_MINUTE ? ", " : "");
			}

			temp = duration / ONE_DAY;
			if (temp > 0) {
				duration -= temp * ONE_DAY;
				res.append(SPACE+temp).append((format.equals(FORMAT.SIMPLE)?resources.getString(R.string.duration_d):temp > 1 ? SPACE+resources.getString(R.string.duration_days) : SPACE+resources.getString(R.string.duration_day)));
			}

		} 
		else 
		{
			res.append(SPACE+resources.getString(R.string.duration_today));
		}
		return res.toString();
	}

}
